#include "sdr.h"
#include "rectype.h"
#include "util.h"
#include "nmfield.h"
#include "nmfield.cpp"

#include "cnfield.h"

#include "nmarrfield.h"
#include "nmarrfield.cpp"

#include "stdffield.h"
#include "stdffield.cpp"

using namespace std;
using namespace opsrc::stdf;

Sdr::Sdr()
{
}

Sdr::Sdr(stdf_rec _stdfrec, int _reclen, std::ios::pos_type _fileRecFirstBytePosition, int _orderindex, endian e, char* buf)
:RecBase(_stdfrec, _reclen, _fileRecFirstBytePosition, _orderindex),en(e) {
    if (buf!=0)
        readFields(buf);
}

Sdr::~Sdr() {
}

const StdfField<int> Sdr::getHeadNum() const {
    return head_num;
}

void Sdr::setHeadNum(StdfField<int> headnum) {
    head_num = headnum;
}

const StdfField<int> Sdr::getSiteGroup() const {
    return site_grp;
}

void Sdr::setSiteGroup(StdfField<int> sitegrp) {
    site_grp = sitegrp;
}

const StdfField<int> Sdr::getSiteCount() const {
    return site_cnt;
}

void Sdr::setSiteCount(StdfField<int> sitecnt) {
    site_cnt = sitecnt;
}

const StdfField<vector<unsigned int> > Sdr::getSiteNum() const {
    return site_num;
}

void Sdr::setSiteNum(StdfField< vector<unsigned int> > sitenum) {
    site_num = sitenum;
}

const StdfField<string> Sdr::getHandlerType() const {
    return hand_typ;
}

void Sdr::setHandlerType(StdfField<string> handtyp) {
    hand_typ = handtyp;
}

const StdfField<string> Sdr::getHandlerId() const {
    return hand_id;
}

void Sdr::setHandlerId(StdfField<string> handid) {
    hand_id = handid;
}

const StdfField<string> Sdr::getProbeCardType() const {
    return card_typ;
}

void Sdr::setProbeCardType(StdfField<string> cardtyp) {
    card_typ = cardtyp;
}

const StdfField<string> Sdr::getProbeCardId() const {
    return card_id;
}

void Sdr::setProbeCardId(StdfField<string> cardid) {
    card_id = cardid;
}

const StdfField<string> Sdr::getLoadBoardType() const {
    return load_typ;
}

void Sdr::setLoadBoardType(StdfField<string> loadtyp) {
    load_typ = loadtyp;
}

const StdfField<string> Sdr::getLoadBoardId() const {
    return load_id;
}

void Sdr::setLoadBoardId(StdfField<string> loadid) {
    load_id = loadid;
}

const StdfField<string> Sdr::getDibBoardType() const {
    return dib_typ;
}

void Sdr::setDibBoardType(StdfField<string> dibtyp) {
    dib_typ = dibtyp;
}

const StdfField<string> Sdr::getDibBoardId() const {
    return dib_id;
}

void Sdr::setDibBoardId(StdfField<string> dibid) {
    dib_id = dibid;
}

const StdfField<string> Sdr::getCableType() const {
    return cabl_typ;
}

void Sdr::setCableType(StdfField<string> cabltyp) {
    cabl_typ = cabltyp;
}

const StdfField<string> Sdr::getCableId() const {
    return cabl_id;
}

void Sdr::setCableId(StdfField<string> cablid) {
    cabl_id = cablid;
}

const StdfField<string> Sdr::getContactorType() const {
    return cont_typ;
}

void Sdr::setContactorType(StdfField<string> conttyp) {
    cont_typ = conttyp;
}

const StdfField<string> Sdr::getContactorId() const {
    return cont_id;
}

void Sdr::setContactorId(StdfField<string> contid) {
    cont_id = contid;
}	

const StdfField<string> Sdr::getLaserType() const {
    return lasr_typ;
}

void Sdr::setLaserType(StdfField<string> lasrtyp) {
    lasr_typ = lasrtyp;
}

const StdfField<string> Sdr::getLaserId() const {
    return lasr_id;
}

void Sdr::setLaserId(StdfField<string> lasrid) {
    lasr_id = lasrid;
}

const StdfField<string> Sdr::getExtraEqptType() const {
    return extr_typ;
}

void Sdr::setExtraEqptType(StdfField<string> extrtyp) {
    extr_typ = extrtyp;
}

const StdfField<string> Sdr::getExtraEqptId() const {
    return extr_id;
}

void Sdr::setExtraEqptId(StdfField<string> extrid) {
    extr_id = extrid;
}	

void Sdr::readFields(char *buf) {
#if _DEBUG
    cout << "stdfrec= SDR , filebytestartposition= " <<   getFileRecByteStartPosition() << ", recbytecount= " << reclen <<endl;
#endif

    int curpos = 0;
    // Read head number , 1 byte
    NmField<unsigned int, 1> headnum(buf+curpos, en);
    head_num.set(headnum.getData(), curpos, headnum.getByteCount(), headnum.getTotalByteCount());
    curpos += head_num.getTotalByteCount();

    // Read site group , 1 byte
    if (curpos < reclen-1) {
        NmField<unsigned int, 1> sitegrp(buf+curpos, en);
        site_grp.set(sitegrp.getData(), curpos, sitegrp.getByteCount(), sitegrp.getTotalByteCount());
        curpos += site_grp.getTotalByteCount();
    }

    // Read site count , 1 byte
    if (curpos < reclen-1) {
        NmField<unsigned int, 1> sitecnt(buf+curpos, en);
        site_cnt.set(sitecnt.getData(), curpos, sitecnt.getByteCount(), sitecnt.getTotalByteCount());
        curpos += site_cnt.getTotalByteCount();
    }

    // Read site num , array of 1 byte number
    if (curpos < reclen-1) {
        NmArrField<unsigned int, 1> sitenum(buf+curpos, en, site_cnt.getData());
        site_num.set(sitenum.getData(), curpos, sitenum.getByteCount(), sitenum.getTotalByteCount());
        curpos += site_num.getTotalByteCount();
    }

    // Read handler type
    if (curpos < reclen -1) {
        CnField<string> handtyp(buf+curpos, en);
        hand_typ.set(handtyp.getData(), curpos, handtyp.getByteCount(), handtyp.getTotalByteCount());
        curpos += hand_typ.getTotalByteCount();
    }

    // Read handler id
    if (curpos < reclen-1) {
        CnField<string> handid(buf+curpos, en);
        hand_id.set(handid.getData(), curpos, handid.getByteCount(), handid.getTotalByteCount());
        curpos += hand_id.getTotalByteCount();
    }

    // Read probecard type
    if (curpos < reclen -1){
        CnField<string> cardtyp(buf+curpos, en);
        card_typ.set(cardtyp.getData(), curpos, cardtyp.getByteCount(), cardtyp.getTotalByteCount());
        curpos += card_typ.getTotalByteCount();
    }

    // Read probecard id
    if (curpos < reclen -1){
        CnField<string> cardid(buf+curpos, en);
        card_id.set(cardid.getData(), curpos, cardid.getByteCount(), cardid.getTotalByteCount());
        curpos += card_id.getTotalByteCount();
    }

        // Read loadboard type
    if (curpos < reclen -1){
        CnField<string> loadtyp(buf+curpos, en);
        load_typ.set(loadtyp.getData(), curpos, loadtyp.getByteCount(), loadtyp.getTotalByteCount());
        curpos += load_typ.getTotalByteCount();
    }

    // Read loadboard id
    if (curpos < reclen -1){
        CnField<string> loadid(buf+curpos, en);
        load_id.set(loadid.getData(), curpos, loadid.getByteCount(), loadid.getTotalByteCount());
        curpos += load_id.getTotalByteCount();
    }

    // Read DIB type
    if (curpos < reclen -1){
        CnField<string> dibtyp(buf+curpos, en);
        dib_typ.set(dibtyp.getData(), curpos, dibtyp.getByteCount(), dibtyp.getTotalByteCount());
        curpos += dib_typ.getTotalByteCount();
    }

    // Read dbi id
    if (curpos < reclen -1){
        CnField<string> dibid(buf+curpos, en);
        dib_id.set(dibid.getData(), curpos, dibid.getByteCount(), dibid.getTotalByteCount());
        curpos += dib_id.getTotalByteCount();
    }

    // Read cable type
    if (curpos < reclen -1){
        CnField<string> cabltyp(buf+curpos, en);
        cabl_typ.set(cabltyp.getData(), curpos, cabltyp.getByteCount(), cabltyp.getTotalByteCount());
        curpos += cabl_typ.getTotalByteCount();
    }

    // Read cable id
    if (curpos < reclen -1){
        CnField<string> cablid(buf+curpos, en);
        cabl_id.set(cablid.getData(), curpos, cablid.getByteCount(), cablid.getTotalByteCount());
        curpos += dib_id.getTotalByteCount();
    }

    // Read contactor type
    if (curpos < reclen -1){
        CnField<string> conttyp(buf+curpos, en);
        cont_typ.set(conttyp.getData(), curpos, conttyp.getByteCount(), conttyp.getTotalByteCount());
        curpos += cont_typ.getTotalByteCount();
    }

    // Read contactor id
    if (curpos < reclen -1){
        CnField<string> contid(buf+curpos, en);
        cont_id.set(contid.getData(), curpos, contid.getByteCount(), contid.getTotalByteCount());
        curpos += cont_id.getTotalByteCount();
    }

    // Read laser type
    if (curpos < reclen -1){
        CnField<string> lasrtyp(buf+curpos, en);
        lasr_typ.set(lasrtyp.getData(), curpos, lasrtyp.getByteCount(), lasrtyp.getTotalByteCount());
        curpos += lasr_typ.getTotalByteCount();
    }

    // Read Laser  id
    if (curpos < reclen -1){
        CnField<string> lasrid(buf+curpos, en);
        lasr_id.set(lasrid.getData(), curpos, lasrid.getByteCount(), lasrid.getTotalByteCount());
        curpos += lasr_id.getTotalByteCount();
    }

    // Read extra equipment  type
    if (curpos < reclen -1){
        CnField<string> extrtyp(buf+curpos, en);
        extr_typ.set(extrtyp.getData(), curpos, extrtyp.getByteCount(), extrtyp.getTotalByteCount());
        curpos += extr_typ.getTotalByteCount();
    }

    // Read Extra Equipment  id
    if (curpos < reclen -1){
        CnField<string> extrid(buf+curpos, en);
        extr_id.set(extrid.getData(), curpos, extrid.getByteCount(), extrid.getTotalByteCount());
    }
}

